<?php
/**
 *
 */
namespace app\common\query;
use app\common\model\MallGoods as MallGoodsModel;
use app\common\cache\CacheData;

class MallGoods
{
    static $status = [
                0 => '下架',
                1 => '上架',
            ];

    public static function status($key=null){
        if(!is_null($key)){
            return self::$status[$key];
        }else{
            return self::$status;
        }
    }
    /**
     * 获取商品列表
     * @param  [type] $where    [description]
     * @param  string $queryStr [description]
     * @param  string $baseUrl  [description]
     * @return [type]           [description]
     */
    public static function getRows($where=[],$order='',$queryStr='',$baseUrl='',$field=''){
        if(!$baseUrl){
            $baseUrl = url('goods.show/index');
        }
        // 每页数量
        $perpagenum = Option::get('perpage_goods_num')?Option::get('perpage_goods_num'):15;
        $rows = self::getDatas($where,$field,$order,$perpagenum);
        $total = $rows['total'];
        $page = $rows['current_page'];
        $rows['page_html'] = pagination($total, $perpagenum, $page, $baseUrl, $queryStr);
        return $rows;
    }

    public static function getGoodsDetail($gid='',$fields=''){

        $model = new MallGoodsModel;

        $rows = $model->where(['id'=>$gid])->field($fields)->find()->toArray();

        $rows = self::setRemark($rows);
        return $rows;
    }

    /**
     * 查询多条数据
     * @return [type] [description]
     */
    public static function getDatas($where=[],$fields='',$order='',$perpagenum=null){
        $model = new MallGoodsModel;
        if($perpagenum){
            $data = $model->where($where)->order('is_tj desc,create_time desc')->field($fields)->order($order)->paginate($perpagenum);
            $data = $data->toArray();
            $rows = $data['data'];
            foreach($rows as &$row):
                $row = self::setRemark($row);
            endforeach;
            $data['data'] = $rows;
        }else{
            $data = $model->where($where)->order('is_tj desc,create_time desc')->field($fields)->order($order)->select();
            $data = $data->toArray();
            foreach($data as &$row):
                $row = self::setRemark($row);
            endforeach;
        }
        return $data;
    }

    /**
     * 处理字段数据
     * @param [type] $row [description]
     */
    public static function setRemark($row){
        if(isset($row['coin_id'])){
            $coin_name = Coin::coinData($row['coin_id']);
            $row['coin_name'] = $coin_name['coin_name'];
        }
        if(isset($row['cate_id'])){
            $cate_name = MallCategory::getCategoryDetail($row['cate_id']);
            $row['cate_name'] = $cate_name['title'];
        }
        if(isset($row['create_time'])){
            $row['create_time'] = date("Y-m-d H:i",$row['create_time']);
        }
        if(isset($row['status'])){
            switch ($row['status']) {
                case '1':
                    $row['is_status'] = '上架';
                    break;
                default:
                    $row['is_status'] = '下架';
                    break;
            }
        }
        if(isset($row['is_tj'])){
            switch ($row['is_tj']) {
                case '1':
                    $row['is_tjs'] = '推荐';
                    break;
                default:
                    $row['is_tjs'] = '普通';
                    break;
            }
        }
        if(isset($row['city'])){
            // 地址处理
            $code = $row['city'];
            // 省
            $row['address_province'] = substr($row['city'], 0,strlen($row['city'])-4);
            // 市
            $row['address_city'] = substr($row['city'], 0,strlen($row['city'])-2);
            // 县
            $row['address_county'] = $row['city'];
        }
        if(isset($row['content'])){
            $row['content'] = strip_tags(htmlspecialchars_decode($row['content']));
        }
        return $row;
    }

    /**
     * 新增商品
     * @param [type] $data [description]
     */
    public function addOneData($data=[]){
        $model = new MallGoodsModel;
        return $model->save($data);
    }
    /**
     * [deleteOneData 删除商品]
     * @param  string $data [description]
     * @return [type]       [description]
     */
    public function deleteOneData($data=''){
        $model = new MallGoodsModel;
        return $model->where(['id'=>$data])->delete();
    }

    /**
     * 更新会员资料
     * @return [type] [description]
     */
    public function saveOneData($data=[],$id=''){
        $model = new MallGoodsModel;
        return $model->save($data,['id' => $id]);
    }

    /**
     * 判断商品库存
     * @return [type] [description]
     */
    public static function goodsTotal($data=[]){
        $model = new MallGoodsModel;
        $res = $model->where($data)->field('id')->find();
        if($res){
           return false;
        }
        return true;
    }

    public static function decTotal($where=[],$row=0){
        $model = new MallGoodsModel;
        return $model->where($where)->setDec('total',$row);
    }

    /**
     * [decTotal 商品数量自增]
     * @param  array   $where [description]
     * @param  integer $row   [description]
     * @return [type]         [description]
     */
    public static function incTotal($where=[],$row=0){
        $model = new MallGoodsModel;
        return $model->where($where)->setInc('total',$row);
    }

    /**
     * [decTotal 自增]
     * @param  array   $where [description]
     * @param  integer $row   [description]
     * @return [type]         [description]
     */
    public static function incNum($where=[],$field='',$row=0){
        $model = new MallGoodsModel;
        return $model->where($where)->setInc($field,$row);
    }

    public static function getValue($where=[],$field=''){
        $model = new MallGoodsModel;
        return $model->where($where)->value($field);
    }
}